package cn.scau.cmi.yeweijie.ORM.DAO;

import cn.scau.cmi.yeweijie.ORM.POJO.Student;
import cn.scau.cmi.yeweijie.ORM.POJO.Teacher;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Iterator;
import java.util.List;

public class TeacherDAO {
    private static SessionFactory factory;
    public TeacherDAO(SessionFactory factory){
        this.factory=factory;
    }
    /* Method to CREATE an employee in the database */
    public Integer addTeacher(int tid, String tname){
        Session session = factory.openSession();
        Transaction tx = null;
        Integer teacherID = null;
        try{
            tx = session.beginTransaction();
            Teacher teacher = new Teacher(tid, tname);
            teacherID = (Integer) session.save(teacher);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
        return teacherID;
    }
    /* Method to  READ all the employees */
    public void listTeacher( ){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            List employees = session.createQuery("FROM Teacher ").list();
            for (Iterator iterator =
                 employees.iterator(); iterator.hasNext();){
                List students = session.createQuery("FROM Student ").list();
                Teacher employee = (Teacher) iterator.next();
                System.out.print("工号: " + employee.getTid());
                System.out.print("  姓名: " + employee.getTname()+"  学生是：");
                for (Iterator iterator1 =
                     students.iterator(); iterator1.hasNext();){
                    Student student = (Student)iterator1.next();
                    if(student.getTid()==employee.getTid())
                        System.out.print(student.getSname()+" ");
                }
            }
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
    /* Method to UPDATE salary for an employee */
    public void updateTeacher(Integer EmployeeID, String tname ){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Teacher employee =
                    (Teacher) session.get(Teacher.class, EmployeeID);
            employee.setTname( tname );
            session.update(employee);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
    /* Method to DELETE an employee from the records */
    public void deleteTeacher(Integer EmployeeID){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Teacher employee =
                    (Teacher) session.get(Teacher.class, EmployeeID);
            session.delete(employee);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }
}
